package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Doctor;
import com.zhiyou100.service.DoctorService;

@Controller
public class DoctorController {
	@Autowired
	private DoctorService docService;
	
	// 查询
	@RequestMapping("/doctor/list")
	public String docList(@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String doc_id,
			@RequestParam(defaultValue="")String doctor_name,
			@RequestParam(defaultValue="")String secco_name,Model model) {
		// 模糊查询
		Map<String,Object> map = new HashMap<>();
		map.put("doc_id", doc_id);
		map.put("doctor_name", doctor_name);
		map.put("secco_name", secco_name);
		// 分页
		PageHelper.startPage(pageNo, 2);
		// 调用服务层
		List<Doctor> doctor = docService.findAll(map);
		PageInfo<Doctor> pageInfo = new PageInfo<>(doctor);
		model.addAttribute("map", map);
		model.addAttribute("doctor", pageInfo.getList());
		model.addAttribute("page", pageInfo);
		System.out.println("医生id: "+doctor);
		return "forward:/view/doctor/docList.jsp";
	}
	// 详情
	@RequestMapping("/doctor/detail")
	public String detailDoctor(Model model,int doc_id) {
		Doctor doctor = docService.findById(doc_id);
		model.addAttribute("doctor", doctor);
		return "forward:/view/doctor/docDetail.jsp";
	}
	
	// 删除
	@RequestMapping("/doctor/delete")
	public String deleteDoctor(int doc_id) {
		// 调用服务层
		docService.deleteById(doc_id);
		return "forward:/doctor/list";
	}
	
	// 添加
	@RequestMapping("/doctor/add")
	public String addDoctor() {
		return "forward:/view/doctor/docAdd.jsp";
	}
	@RequestMapping("/doctor/add2")
	public String add2Doctor(Doctor doctor) {
		docService.addDoctor(doctor);
		return "forward:/doctor/list";
	}
	
	
	// 更新 查询
	@RequestMapping("/doctor/edit")
	public String findToEdit(int doc_id,Model model) {
		Doctor doctor = docService.findTodit(doc_id);
		System.out.println("更新向页面发送的doctor: "+doctor);
		model.addAttribute("doctor", doctor);
		return "forward:/view/doctor/docEdit.jsp";
	}
	@RequestMapping("/doctor/update")
	public String updateDoctor(Doctor doctor) {
		docService.updateDoctor(doctor);
		System.out.println("更新从页面接受doctor: "+doctor);
		return "forward:/doctor/list";
	}
}
